<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="贷款流水号" prop="loanSerialNo">
        <el-input
          v-model="queryParams.loanSerialNo"
          placeholder="请输入贷款流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户号" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入客户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="趸交" prop="isSinglePayment">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isSinglePayment"-->
      <!--          placeholder="请输入趸交"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="期数" prop="periodNum">
        <el-input
          v-model="queryParams.periodNum"
          placeholder="请输入期数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="滞纳金按日收费宽限期" prop="lateFeeGracePeriod">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.lateFeeGracePeriod"-->
      <!--          placeholder="请输入滞纳金按日收费宽限期"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="还款日期" prop="repayDate">
        <el-input
          v-model="queryParams.repayDate"
          placeholder="请输入还款日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借据状态" prop="iouStatus">
        <el-select v-model="queryParams.iouStatus" placeholder="请选择借据状态" clearable size="small">
          <el-option
            v-for="dict in iouStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结清时间" prop="settleDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.settleDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择结清时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="create date">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="start time"
          end-placeholder="End time"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:LoanIou:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:LoanIou:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:LoanIou:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:LoanIou:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LoanIouList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" width="100" prop="id"/>
      <el-table-column label="LoanSerialNo" align="center" width="250" prop="loanSerialNo"/>
      <el-table-column label="CustNo" align="center" width="200" prop="custNo"/>
      <el-table-column label="LoanAmt" align="center" prop="loanAmt"/>
      <el-table-column label="IsSinglePayment" align="center" width="130" prop="isSinglePayment"/>
      <el-table-column label="PeriodNum" align="center" width="100" prop="periodNum"/>
      <!--是否延期-->
      <el-table-column label="是否延期" align="center" width="100" prop="isDeferred"/>
      <el-table-column label="延期天数（累计）" align="center" width="100" prop="deferredPeriodNum"/>
      <el-table-column label="延期到期日" align="center" width="100" prop="deferredRepayDate"/>


      <el-table-column label="Unit" align="center" width="80" prop="unit"/>
      <el-table-column label="InterestRate" align="center" width="110" prop="interestRate"/>
      <el-table-column label="DateLateFee" align="center" width="110" prop="dateLateFee"/>
      <el-table-column label="LateFeeGracePeriod" align="center" width="150" prop="lateFeeGracePeriod"/>
      <el-table-column label="LateFeeMaxDays" align="center" width="130" prop="lateFeeMaxDays"/>
      <el-table-column label="LoanPrincipal" align="center" width="130" prop="loanPrincipal"/>
      <el-table-column label="RepaidPrincipal" align="center" width="130" prop="repaidPrincipal"/>
      <el-table-column label="RepayDate" align="center" width="120" prop="repayDate"/>
      <el-table-column label="ServiceFee" align="center" width="110" prop="serviceFee"/>
      <el-table-column label="RepaidServiceFee" align="center" width="140" prop="repaidServiceFee"/>
      <el-table-column label="LoanInterest" align="center" width="110" prop="loanInterest"/>
      <el-table-column label="RepaidInterest" align="center" width="120" prop="repaidInterest"/>
      <el-table-column label="FeeRemissionAmt" align="center" width="140" prop="feeRemissionAmt"/>
      <el-table-column label="LateFee" align="center" width="180" prop="lateFee"/>
      <el-table-column label="RepaidLateFee" align="center" width="120" prop="repaidLateFee"/>
      <el-table-column label="OverdueAmount" align="center" width="130" prop="overdueAmount"/>
      <el-table-column label="ReduceAmt" align="center" width="130" prop="reduceAmt"/>
      <el-table-column label="SettleDate" align="center" width="180" prop="settleDate"/>
      <el-table-column label="IouStatus" align="center" width="100" prop="iouStatus" :formatter="iouStatusFormat"/>
      <el-table-column label="CreateTime" align="center" prop="createTime" width="180" />
      <el-table-column label="UpdateTime" align="center" prop="updateTime" width="180" />
      <el-table-column label="State" align="center" width="80" prop="state" :formatter="stateFormat"/>
      <el-table-column label="Remark" align="center" width="150" prop="remark"/>
      <el-table-column label="Operation" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.iouStatus != 1 && ( scope.row.numFinance == 0 || scope.row.numFinance == null )   "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="RepaymentQuery(scope.row)"
          >还款查询
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:LoanIou:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:LoanIou:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改贷款借据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="贷款流水号" prop="loanSerialNo">
          <el-input v-model="form.loanSerialNo" placeholder="请输入贷款流水号"/>
        </el-form-item>
        <el-form-item label="客户号" prop="custNo">
          <el-input v-model="form.custNo" placeholder="请输入客户号"/>
        </el-form-item>
        <el-form-item label="借款金额" prop="loanAmt">
          <el-input v-model="form.loanAmt" placeholder="请输入借款金额"/>
        </el-form-item>
        <el-form-item label="趸交" prop="isSinglePayment">
          <el-input v-model="form.isSinglePayment" placeholder="请输入趸交"/>
        </el-form-item>
        <el-form-item label="期数" prop="periodNum">
          <el-input v-model="form.periodNum" placeholder="请输入期数"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="利(息)率" prop="interestRate">
          <el-input v-model="form.interestRate" placeholder="请输入利(息)率"/>
        </el-form-item>
        <el-form-item label="滞纳金日费率" prop="dateLateFee">
          <el-input v-model="form.dateLateFee" placeholder="请输入滞纳金日费率"/>
        </el-form-item>
        <el-form-item label="滞纳金按日收费宽限期" prop="lateFeeGracePeriod">
          <el-input v-model="form.lateFeeGracePeriod" placeholder="请输入滞纳金按日收费宽限期"/>
        </el-form-item>
        <el-form-item label="滞纳金计算最大天数" prop="lateFeeMaxDays">
          <el-input v-model="form.lateFeeMaxDays" placeholder="请输入滞纳金计算最大天数"/>
        </el-form-item>
        <el-form-item label="放款本金" prop="loanPrincipal">
          <el-input v-model="form.loanPrincipal" placeholder="请输入放款本金"/>
        </el-form-item>
        <el-form-item label="已还本金" prop="repaidPrincipal">
          <el-input v-model="form.repaidPrincipal" placeholder="请输入已还本金"/>
        </el-form-item>
        <el-form-item label="还款日期" prop="repayDate">
          <el-input v-model="form.repayDate" placeholder="请输入还款日期"/>
        </el-form-item>
        <el-form-item label="服务费" prop="serviceFee">
          <el-input v-model="form.serviceFee" placeholder="请输入服务费"/>
        </el-form-item>
        <el-form-item label="已还服务费" prop="repaidServiceFee">
          <el-input v-model="form.repaidServiceFee" placeholder="请输入已还服务费"/>
        </el-form-item>
        <el-form-item label="总利息" prop="loanInterest">
          <el-input v-model="form.loanInterest" placeholder="请输入总利息"/>
        </el-form-item>
        <el-form-item label="已还总利息" prop="repaidInterest">
          <el-input v-model="form.repaidInterest" placeholder="请输入已还总利息"/>
        </el-form-item>
        <el-form-item label="减免金额" prop="feeRemissionAmt">
          <el-input v-model="form.feeRemissionAmt" placeholder="请输入减免金额"/>
        </el-form-item>
        <el-form-item label="滞纳金" prop="lateFee">
          <el-input v-model="form.lateFee" placeholder="请输入滞纳金"/>
        </el-form-item>
        <el-form-item label="已还滞纳金" prop="repaidLateFee">
          <el-input v-model="form.repaidLateFee" placeholder="请输入已还滞纳金"/>
        </el-form-item>
        <el-form-item label="逾期金额" prop="overdueAmount">
          <el-input v-model="form.overdueAmount" placeholder="请输入逾期金额"/>
        </el-form-item>
        <el-form-item label="借据状态" prop="iouStatus">
          <el-select v-model="form.iouStatus" placeholder="请选择借据状态">
            <el-option
              v-for="dict in iouStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结清时间" prop="settleDate">
          <el-date-picker clearable size="small"
                          v-model="form.settleDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择结清时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 还款查询的弹窗 -->
    <el-dialog title="选择虚拟账户" :close-on-click-modal="false" :show-close="false" width="300px" :visible.sync="RepaymentStatus" class="RepaymentMsg">
      <el-dialog
        :close-on-click-modal="false"
        :show-close="false"
        v-loading="RepaymentListLoading"
        class="RepaymentMsg"
        width="40%"
        min-width='500px'
        title="还款查询结果"
        :visible.sync="RepaymentList"
        append-to-body>
        <el-table
          :data="RepaymentListTable"
          style="width: 100%">
          <el-table-column
            prop="reference"
            label="PayTransSerialNo"
            width="260">
          </el-table-column>
          <el-table-column
            prop="amountPaid"
            label="金额"
            width="180">
          </el-table-column>
          <el-table-column
            :formatter='typeStatus'
            prop="type"
            label="结果">
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="RepaymentList = false">取 消</el-button>
          <el-button type="primary" @click="RepaymentListSubmit()">确定</el-button>
        </div>
      </el-dialog>
      <el-select v-model="accountReference" placeholder="请选择">
        <el-option
          v-for="item in RepaymentOptions"
          :key="item.accountsAccountNumber"
          :label="item.accountsAccountNumber"
          :value="item.accountsAccountNumber">
        </el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="RepaymentStatus = false">取 消</el-button>
        <el-button type="primary" @click="RepaymenSubmit()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {doMonniflyFinance,extMonnifyReserved,listLoanIou, getLoanIou, delLoanIou, addLoanIou, updateLoanIou, exportLoanIou} from "@/api/business/LoanIou";

export default {
  name: "LoanIou",
  components: {},
  data() {
    return {
      RepaymentListLoading:false,
      //选择的虚拟号
      accountReference:'',
      rowsData:'',
      RepaymentListTable:[{
        "reference": "reference编号",
        "time": "时间",
        "amountPaid": "金额",
        "type": "1"// 进入溢缴款
      }, {
        "reference": "reference编号2",
        "time": "时间2",
        "amountPaid": "金额2",
        "type": "2"//进入冲销
      }],
      RepaymentList:false,
      RepaymentOptions: [],
      RepaymentStatus:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 贷款借据表格数据
      LoanIouList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 借据状态字典
      iouStatusOptions: [],
      // 状态字典
      stateOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        loanSerialNo: null,
        custNo: null,
        isSinglePayment: null,
        periodNum: null,
        unit: null,
        lateFeeGracePeriod: null,
        repayDate: null,
        iouStatus: null,
        settleDate: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        loanSerialNo: [
          {required: true, message: "贷款流水号不能为空", trigger: "blur"}
        ],
        custNo: [
          {required: true, message: "客户号不能为空", trigger: "blur"}
        ],
        loanAmt: [
          {required: true, message: "借款金额不能为空", trigger: "blur"}
        ],
        repayDate: [
          {required: true, message: "还款日期不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    // this.getList();
    this.getDicts("loan_iou_status").then(response => {
      this.iouStatusOptions = response.data;
    });
    this.getDicts("state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    RepaymentListSubmit(){
      this.RepaymentList = false
      this.RepaymentStatus = false
    },
    typeStatus(row){
      if(row == '1'){
        return '进入溢缴款'
      }else if(row == '2'){
        return '进入冲销'
      }
    },
    RepaymenSubmit(){
      this.RepaymentList =  true
      this.RepaymentListLoading = true
      var main = this
      doMonniflyFinance({
        'loanSerialNo':this.rowsData.loanSerialNo,
        'accountReference':this.accountReference
      }).then(response => {
        main.RepaymentListLoading = false
        main.accountReference = ''
        main.RepaymentListTable = response.resList
      });
    },
    // 还款查询弹窗
    RepaymentQuery(row){
      console.info(JSON.stringify(row))
      this.rowsData = row
      this.extMonnifyReservedFun({custNo:row.custNo})
    },
    extMonnifyReservedFun(data){
      console.log(JSON.stringify(data))
      var main = this
      extMonnifyReserved(data).then(response => {
        main.RepaymentOptions = response.rows
        if(response.rows.length !=0){
          // 数据不为空时展示下拉框
          main.RepaymentStatus = true
        }
      });
    },
    /** 查询贷款借据列表 */
    getList() {
      this.loading = true;
      listLoanIou(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.LoanIouList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 借据状态字典翻译
    iouStatusFormat(row, column) {
      return this.selectDictLabel(this.iouStatusOptions, row.iouStatus);
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        loanSerialNo: null,
        custNo: null,
        loanAmt: null,
        isSinglePayment: null,
        periodNum: null,
        unit: null,
        interestRate: null,
        dateLateFee: null,
        lateFeeGracePeriod: null,
        lateFeeMaxDays: null,
        loanPrincipal: null,
        repaidPrincipal: null,
        repayDate: null,
        serviceFee: null,
        repaidServiceFee: null,
        loanInterest: null,
        repaidInterest: null,
        feeRemissionAmt: null,
        lateFee: null,
        repaidLateFee: null,
        overdueAmount: null,
        iouStatus: null,
        settleDate: null,
        state: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        dateRange: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.dateRange = [];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加贷款借据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLoanIou(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改贷款借据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLoanIou(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLoanIou(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除贷款借据编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delLoanIou(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有贷款借据数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportLoanIou(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style type="text/css">
.RepaymentMsg .el-dialog .el-dialog__header{
  text-align: center;
}
.RepaymentMsg .el-dialog .el-dialog__body .el-select{
  width: 100%;
}
.RepaymentMsg .el-dialog .el-dialog__footer{
  text-align: center;
}
</style>
